package com.drojian.demo4.communication;

import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.drojian.demo.R;
import com.drojian.demo4.AlertDialogUtils;
import com.drojian.demo4.DialogInputCallback;

public class ContentProviderActivity extends AppCompatActivity implements View.OnClickListener {
    private Button add,delete,update,query;
    private TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_content_provider);
        initView();
    }

    private void initView() {
        add = findViewById(R.id.add);
        delete = findViewById(R.id.delete);
        update = findViewById(R.id.update);
        query = findViewById(R.id.query);
        text = findViewById(R.id.text);
        add.setOnClickListener(this);
        delete.setOnClickListener(this);
        update.setOnClickListener(this);
        query.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add:
                AlertDialogUtils.showDialog(this, "添加", "请输入ID", "请输入Name", new DialogInputCallback() {
                    @Override
                    public void onInputConfirmed(String inputId, String inputName) {
                        Uri uri = Uri.parse("content://com.drojian.provider.provider/book");
                        ContentResolver contentResolver = getContentResolver();
                        ContentValues values = new ContentValues();
                        values.put("id", inputId);
                        values.put("name", inputName);
                        contentResolver.insert(uri, values);
                    }
                });
                Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                AlertDialogUtils.showDialog(this, "删除", "请输入ID", "请输入Name", new DialogInputCallback() {
                    @Override
                    public void onInputConfirmed(String inputId, String inputName) {
                        Uri uri = Uri.parse("content://com.drojian.provider.provider/book");
                        ContentResolver contentResolver = getContentResolver();
                        contentResolver.delete(uri, "id=?",new String[]{inputId});
                    }
                });
                Toast.makeText(this,"删除成功",Toast.LENGTH_SHORT).show();
                break;
            case R.id.update:
                AlertDialogUtils.showDialog(this, "修改", "请输入ID", "请输入Name", new DialogInputCallback() {
                    @Override
                    public void onInputConfirmed(String inputId, String inputName) {
                        Uri uri = Uri.parse("content://com.drojian.provider.provider/book");
                        ContentResolver contentResolver = getContentResolver();
                        ContentValues values = new ContentValues();
                        values.put("id", inputId);
                        values.put("name", inputName);
                        contentResolver.update(uri, values,"id=?",new String[]{inputId});
                    }
                });
                Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();

                break;
            case R.id.query:
                ContentResolver contentResolver = this.getContentResolver();
                Uri uri = Uri.parse("content://com.drojian.provider.provider/book");
                Cursor cursor = contentResolver.query(uri, null, null,
                        null,null,null);
                StringBuilder builder = new StringBuilder();
                if(cursor.moveToFirst()){
                    do{
                        int column_age = cursor.getColumnIndex("id");
                        int id = cursor.getInt(column_age);
                        int column_name = cursor.getColumnIndex("name");
                        String name = cursor.getString(column_name);
                        builder.append("id:"+id+"   "+"name:"+name+"\n");
                    }while (cursor.moveToNext());
                }
                cursor.close();
                text.setText(builder);
                Toast.makeText(this,"查询成功",Toast.LENGTH_SHORT).show();

                break;
        }
    }
}